package com.test.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.deploy.net.HttpResponse;
import com.test.controller.Admininfo.ResData;
import com.test.entity.LeaderInfo;
import com.test.service.DepartInfoService;
import com.test.service.LeaderInfoService;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 管理员(AdminInfo)表控制层
 *
 * @author makejava
 * @since 2022-11-12 11:18:50
 */
@Controller
public class AdminInfoController {
    /**
     * 领导信息
     */
    @Resource
    private LeaderInfoService leaderInfoService;
    /**
     * 部门信息
     */
    @Resource
    private DepartInfoService departInfoService;
//    显示admin的index首页
    @RequestMapping("admin/index")
    public String getIndex(){
        return "admin/index";
    }
//    显示部门领导管理
    @GetMapping("admin/leader_info")
    public ModelAndView getLeaderInfo(){
        ModelAndView modelAndView = new ModelAndView("admin/leader_info/list");
        modelAndView.addObject("departInfoList",departInfoService.selectAll());
        return modelAndView;
    }
//    显示所有的领导信息
    @GetMapping("admin/leader_info/queryList")
    @ResponseBody
    public String  getQueryList(Integer page, LeaderInfo leaderInfo, HttpServletResponse response) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(leaderInfoService.queryByPage(leaderInfo, PageRequest.of(page-1,10)));
    }
    //    跳转新增领导信息
    @GetMapping("admin/leader_info/add")
    public ModelAndView getAdd(){
        ModelAndView modelAndView = new ModelAndView("admin/leader_info/add_page");
        modelAndView.addObject("departInfoList",departInfoService.selectAll());
        return modelAndView;
    }
    //    提交新增领导信息
    @PostMapping("admin/leader_info/add_submit")
    @ResponseBody
    public ResData postAddSubmit(LeaderInfo leaderInfo){
        Integer insert = leaderInfoService.insert(leaderInfo);

        ResData resData = new ResData();
        if (insert>0){
            resData.setCode(200);
            resData.setMsg("添加成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("添加失败");
        return resData;
    }
    //    跳转修改领导信息
    @GetMapping("admin/leader_info/update")
    public ModelAndView getUpdate(Integer id){
        ModelAndView modelAndView  = new ModelAndView("admin/leader_info/update_page");
        LeaderInfo leaderInfo = leaderInfoService.queryById(id);
        modelAndView.addObject("data",leaderInfo);
        modelAndView.addObject("departInfoList",departInfoService.selectAll());
        return modelAndView;
    }
    //    提交修改领导信息
    @PostMapping("admin/leader_info/update_submit")
    @ResponseBody
    public ResData postUpdateSubmit(Integer id ,LeaderInfo leaderInfo){
        leaderInfo.setId(id);
        LeaderInfo update = leaderInfoService.update(leaderInfo);

        ResData resData = new ResData();

        if (!update.getId().equals("") || update.getId()!=null){
            resData.setCode(200);
            resData.setMsg("修改成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("修改失败");
        return resData;
    }
    //    删除领导信息
    @GetMapping("admin/leader_info/del")
    @ResponseBody
    public ResData getDelete(Integer id){
        leaderInfoService.deleteById(id);
        ResData resData = new ResData();
        resData.setCode(1);
        resData.setMsg("删除成功");
        return resData;
    }

}

